// JEEA Public Disclosure .do files - Appendix Tables A1-A9 //

clear all
set more off
set matsize 1000

timer clear
timer on 1

cd "C:/Users/tjb0217/Box Sync/Tyler/Nigeria Malaria/JEEA/JEEA Public Disclosure - 04062020"
	global wber `c(pwd)'
	global data "$wber\data"
	global output "$wber\output"



// Prepping for table 1
use "${data}\workerbyweek.dta", clear
keep workerID gang week_earn attendance daysworked total_amount wage scrabbler 
reshape wide attendance daysworked total_amount wage scrabbler , i(workerID gang) j(week_earn)
sort workerID gang

gen lastwork=8 if attendance8!=0
replace lastwork=8 if attendance8!=0 
replace lastwork=7 if attendance7!=0 & attendance8==0 
replace lastwork=6 if attendance6!=0 & attendance7==0 & attendance8==0 
replace lastwork=5 if attendance5!=0 & attendance6!=0 & attendance7==0 & attendance8==0
replace lastwork=4 if attendance4!=0 & attendance5!=0 & attendance6!=0 & attendance7==0 & attendance8==0
replace lastwork=3 if attendance3!=0 & attendance4!=0 & attendance5!=0 & attendance6!=0 & attendance7==0 & attendance8==0
replace lastwork=2 if attendance2!=0 & attendance3!=0 & attendance4!=0 & attendance5!=0 & attendance6!=0 & attendance7==0 & attendance8==0
replace lastwork=1 if attendance1!=0 & attendance2!=0 & attendance3!=0 & attendance4!=0 & attendance5!=0 & attendance6!=0 & attendance7==0 & attendance8==0
** ^ all workers accounted for at this point


gen attrit=[lastwork!=8]
tab attrit // % of workers attrited (including not working in week 15 as attriting)

tempfile attrition
save `attrition'



**Table A3 - Comparison of attrited and non-attrited workers **
use "$data\balance.dta", clear

merge 1:1 workerID using `attrition'
drop _m

global depvars "age ln_exp_year edyrs BMI hhsize_p rooms_p n_cattle_p n_poultry_p hhasset_m1D"

foreach i in $depvars {
	sum `i' if attrit == 0
	matrix N`i'=r(N)
	matrix m`i'=r(mean)
	matrix sd`i'=r(sd)
	matrix `i'=N`i', m`i', sd`i'
	matrix rownames `i'=`i'
	}

matrix table_1=age\ln_exp_year\edyrs\BMI\hhsize_p\rooms_p\n_cattle_p\n_poultry_p\hhasset_m1D

frmttable using "$output\Appendix\TableA3.doc", replace sdec(0, 2, 2) ///
statmat(table_1) varlabels title("Table A3") coljust(l;c) ctitles("" ,N , Mean, SD)	

foreach i in $depvars {
	sum `i' if attrit == 1
	matrix N`i'=r(N)
	matrix m`i'=r(mean)
	matrix sd`i'=r(sd)
	matrix `i'=N`i', m`i', sd`i'
	matrix rownames `i'=`i'
	}

matrix table_2=age\ln_exp_year\edyrs\BMI\hhsize_p\rooms_p\n_cattle_p\n_poultry_p\hhasset_m1D

frmttable using "$output\Appendix\TableA3.doc", append sdec(0, 2, 2) ///
statmat(table_2) varlabels title("Table A3") coljust(l;c) ctitles("" ,N , Mean, SD)	



foreach i in $depvars {
reg `i' attrit
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,9]
testparm attrit
local pvalue_all=r(p)
eststo `i'_2, addscalars(pvalue_all `pvalue_all' cons_all `cons_all')
}
xml_tab *_2, save($output\Appendix\TableA3.xls) stats(N pvalue_all cons_all) title(T1) sd2 replace sheet(T1 - all) format(SCLR3 NCLR3) //	keep(*gang )


eststo clear

** Table A5A Robust sub-sample analysis of TmUT **
// Panel A - only workers-weeks with no days devoted to scrabbling //
use "$data\dailydata.dta", clear
keep if scrabbledays == 0 	

foreach outcome in attendance lnearnings {

** One week reference 
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE4

}

foreach outcome in attendance lnearnings {

xml_tab `outcome'_TmUTdayFE1 `outcome'_TmUTdayFE2 `outcome'_TmUTdayFE3 `outcome'_TmUTdayFE4, ///
save($output\Appendix\`outcome'_A5A_TmUT_IFENoScrabble.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}


*Weekly workers-weeks with no scrabbling days
*now looking at the one week gains for those who tested negative for malaria
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
keep if scrabbledays == 0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains from treatment

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
keep if scrabbledays == 0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains from treatment

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
keep if scrabbledays == 0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	
*now looking at the four week gains from treatment

local i = 2
while `i' <= 4 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
keep if scrabbledays == 0
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT4week

}

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week `outcome'_TMUT4week, ///
	save($output/Appendix/`outcome'_A5A_TMUTNoScrabble.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

// Table A5B - Robus sub-sample analysis of TMUT continued //
// Panel B: Only workers who report no recent illness //

*Daily workers with no recent illness

use "$data\dailydata.dta", clear
keep if anymorbid == 0 	
foreach outcome in attendance lnearnings {

** One week reference 
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE4

}

foreach outcome in attendance lnearnings {

xml_tab `outcome'_TmUTdayFE1 `outcome'_TmUTdayFE2 `outcome'_TmUTdayFE3 `outcome'_TmUTdayFE4, ///
save($output/Appendix/`outcome'_A5B_TmUT_IFEIllness.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}


*Weekly workers with no recent illness
*now looking at the one week gains for those who tested negative for malaria
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & anymorbid==0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains from treatment

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & anymorbid==0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains from treatment

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & anymorbid==0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	
*now looking at the four week gains from treatment

local i = 2
while `i' <= 4 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & anymorbid==0
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT4week

}

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week `outcome'_TMUT4week, ///
	save($output/Appendix/`outcome'_A5B_TMUTIllness.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

** Table A4: TOT for workers testing positive for malaria by parasite count

** Daily TOT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

g cond_scrabble=0 if attendance==1
replace cond_scrabble=. if attendance==0 | attendance==. 
replace cond_scrabble=1 if total_amount==500 | total_amount==550 


foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 1 & para_max==3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 1 & para_max==3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 1 & para_max==3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 1 & para_max==3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A4TOT_IFEp3.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 1 & para_max>3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 1 & para_max>3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 1 & para_max>3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 1 & para_max>3, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A4TOT_IFEp4.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}


**Between Worker--Weekly Results**

*now looking at the one week gains for those who had a parasite count of 3
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max==3
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	sort workerID
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT1week

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max==3
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT2week

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max==3
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT3week

}
foreach outcome in lndays lnearnings lnwage scrabbleratio {

xml_tab `outcome'_TOT1week `outcome'_TOT2week `outcome'_TOT3week, ///
	save($output/Appendix/`outcome'_A4Para3TOT.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

*now looking at the one week gains for those who had a parasite count of at least 4
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max>=4
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	sort workerID
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT1week

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max>=4
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT2week

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 1 & para_max>=4
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT3week

}
foreach outcome in lndays lnearnings lnwage scrabbleratio {

xml_tab `outcome'_TOT1week `outcome'_TOT2week `outcome'_TOT3week, ///
	save($output/Appendix/`outcome'_A4Para4TOT.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

** Table A6B: TmUT for workers testing negative for malaria by parasite count and fatigue
*now looking at the one week gains for workers with a parasite count of 0
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & para_max == 0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & para_max == 0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & para_max == 0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage  {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week, ///
	save($output/Appendix/`outcome'_A6B_TMUTPara0.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear


*now looking at the one week gains for workers with a parasite count of 1 or 2
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & (para_max == 1 | para_max == 2)
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & (para_max == 1 | para_max == 2)
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & (para_max == 1 | para_max == 2)
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage  {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week, ///
	save($output/Appendix/`outcome'_A6B_TMUTPara1.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear


*now looking at the one week gains for workers who are not tired at the end of the day
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage  {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week, ///
	save($output/Appendix/`outcome'_A6B_TMUTNotTired.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

*now looking at the one week gains for workers who are tired at the end of the day
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==1
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==1
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0 & tired==1
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage  {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	

foreach outcome in lndays lnearnings lnwage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week, ///
	save($output/Appendix/`outcome'_A6B_TMUTTired.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

**A6A:  Within Worker TmuT Estimates for parasite count and fatigue
// Within worker estimates using daily data //
** Daily TmuT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

g cond_scrabble=0 if attendance==1
replace cond_scrabble=. if attendance==0 | attendance==. 
replace cond_scrabble=1 if total_amount==500 | total_amount==550 

**Parasite Count==0**

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0 & para_max==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0 & para_max==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0 & para_max==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0 & para_max==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings  cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A6ATMUT_IFEp0.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}

**Parasite Count 1 or 2**

g para12=0
replace para12=1 if para_max==1
replace para12=1 if para_max==2

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0 & para12==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0 & para12==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0 & para12==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0 & para12==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A6ATMUT_IFEp12.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

} 

**Fatigue Results**

**TIRED**

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0 & tired==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0 & tired==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0 & tired==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0 & tired==1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A6ATMUT_IFETired.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}

**NOT TIRED**

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0 & tired==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0 & tired==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0 & tired==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0 & tired==0, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_A6ATMUT_IFENOTTIRED.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

} 


**A6B:  Between Worker TmuT Estimates for parasite count and fatigue
// estimates by malaria negative worker parasite count or fatigue status at end of day (continued) //

use "$data/workerbyweek.dta", clear

g para12=0
replace para12=1 if para_max==1
replace para12=1 if para_max==2

save "$data/workerbyweek.dta", replace


*Weekly workers-weeks with no scrabbling days
*now looking at the one week gains for those who tested negative for malaria
local i = 2
while `i' <= 7 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
// keep if scrabbledays == 0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

local cond1 "& para_max == 0"
local cond2 "& para12 == 1"

local cond3 "& tired == 0"
local cond4 "& tired == 1"

forvalues x = 1/4 {
	foreach outcome in lndays lnearnings lnwage scrabbleratio {

		di "`outcome'"
		reg `outcome' T_w1 if studyweeks == 1 `cond`x'', vce(cluster workerXround)
		reg `outcome' T_w1 gangBYweek* if studyweeks == 1 `cond`x'', vce(cluster workerXround)
		estimates store `outcome'_TMUT1week`x'
		}
}



*now looking at the two week gains from treatment

local i = 2
while `i' <= 6 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
// keep if scrabbledays == 0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

forvalues x = 1/4 {
	foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1 `cond`x'', vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1 `cond`x'', vce(cluster workerXround)
estimates store `outcome'_TMUT2week`x'

}
}

*now looking at the three week gains from treatment

local i = 2
while `i' <= 5 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
// keep if scrabbledays == 0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

forvalues x = 1/4 {
	foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1 `cond`x'', vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1 `cond`x'', vce(cluster workerXround)
estimates store `outcome'_TMUT3week`x'

}
}

*now looking at the four week gains from treatment

local i = 2
while `i' <= 4 {
use "$data/workerbyweek.dta", clear
keep if mal_pos == 0
// keep if scrabbledays == 0
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data/temp.dta", replace
	}
if `i' > 2 {
	append using "$data/temp.dta"
	save "$data/temp.dta", replace
	}
local i = `i' + 1
sleep 1000
}

forvalues x = 1/4 {
	foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1 `cond`x'', vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1 `cond`x'', vce(cluster workerXround)
estimates store `outcome'_TMUT4week`x'

}
}

forvalues x = 1/4 {
	foreach outcome in lndays lnearnings lnwage scrabbleratio {
		if `x' == 1 {
			xml_tab `outcome'_TMUT1week`x' `outcome'_TMUT2week`x' `outcome'_TMUT3week`x' `outcome'_TMUT4week`x', save($output/Appendix/`outcome'_TMUT_A6B_p0.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)
		}
		if `x' == 2 {
			xml_tab `outcome'_TMUT1week`x' `outcome'_TMUT2week`x' `outcome'_TMUT3week`x' `outcome'_TMUT4week`x', save($output/Appendix/`outcome'_TMUT_A6B_p12.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)
		}
		if `x' == 3 {
			xml_tab `outcome'_TMUT1week`x' `outcome'_TMUT2week`x' `outcome'_TMUT3week`x' `outcome'_TMUT4week`x', save($output/Appendix/`outcome'_TMUT_A6B_NotTired.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)
		}
		if `x' == 4 {
			xml_tab `outcome'_TMUT1week`x' `outcome'_TMUT2week`x' `outcome'_TMUT3week`x' `outcome'_TMUT4week`x', save($output/Appendix/`outcome'_TMUT_A6B_Tired.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)
		}

}
}

estimates clear

***************************
** Table A7:  ITT Results in levels **
***************************
** Daily ITT results **
	
use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in total_amount {

di "`outcome'"

** One week gains
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9, vce(cluster workerID) 
estimates store `outcome'_ITTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16, vce(cluster workerID)
estimates store `outcome'_ITTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23, vce(cluster workerID)
estimates store `outcome'_ITTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30, vce(cluster workerID)
estimates store `outcome'_ITTdayFE4

}


foreach outcome in total_amount {

xml_tab `outcome'_ITTdayFE1 `outcome'_ITTdayFE2 `outcome'_ITTdayFE3 `outcome'_ITTdayFE4, ///
save($output/Appendix/`outcome'_TA7_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}


** Weekly ITT results **
*now looking at the one week gains from treatment
use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis


local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	sort workerID
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

// foreach outcome in total_amount daysworked wage {
foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet1

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & ///
(week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet2

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & ///
(week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {


di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet3

}


*now looking at the four week gains from treatment
local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet4

}


foreach outcome in total_amount daysworked wage {

xml_tab `outcome'_T4_bet1 `outcome'_T4_bet2 `outcome'_T4_bet3 `outcome'_T4_bet4, ///
	save($output/Appendix/`outcome'_TA7_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T* )

}

estimates clear




	
**************************
** Table A8: TOT results for workers testing positive for malaria in levels **
**************************
** Daily TOT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in total_amount {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in total_amount {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/Appendix/`outcome'_TA8_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}


** Weekly TOT results **
*now looking at the one week gains for those who tested positive for malaria
local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	sort workerID
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT1week

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT2week

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT3week

}


*now looking at the four week gains from treatment
local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT4week

}


foreach outcome in total_amount daysworked wage {

xml_tab `outcome'_TOT1week `outcome'_TOT2week `outcome'_TOT3week `outcome'_TOT4week, ///
	save($output/Appendix/`outcome'_TA8_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear


***************************
** Table A9: TmUT results for workers testing negative for malaria in levels **
***************************
** Daily TmUT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in total_amount {

** One week reference 
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE4

}

foreach outcome in total_amount {

xml_tab `outcome'_TmUTdayFE1 `outcome'_TmUTdayFE2 `outcome'_TmUTdayFE3 `outcome'_TmUTdayFE4, ///
save($output/Appendix/`outcome'_TA9_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}


** Weekly TmUT results **
*now looking at the one week gains for those who tested negative for malaria
local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains from treatment

local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains from treatment

local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	
*now looking at the four week gains from treatment

local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in total_amount daysworked wage {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT4week

}

foreach outcome in total_amount daysworked wage {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week `outcome'_TMUT4week, ///
	save($output/Appendix/`outcome'_TA9_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear




use "$data/workerbyweek.dta", clear

drop para12

save "$data/workerbyweek.dta", replace



erase "$data/temp.dta"

timer off 1
timer list 1
noi di `r(tl)' 


